package com.xianzhi.controller.content;

import com.xianzhi.constant.PageCodeEnum;
import com.xianzhi.dto.AdDto;
import com.xianzhi.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/ad")
public class AdController {
    //注入service
    @Autowired
    private AdService adService;

    @RequestMapping
    public String ad(ModelMap modelMap){
        AdDto adDto=new AdDto();
        List<AdDto> list = adService.searchByPage(adDto);
        modelMap.addAttribute("list",list);
        return "/content/adList";
    }

    @RequestMapping("/search")
    public String search(ModelMap modelMap,AdDto adDto){
        modelMap.addAttribute("list",adService.searchByPage(adDto));
        return "/content/adList";
    }

    @RequestMapping("/addInit")
    public String addInit(){
        return "/content/adAdd";
    }

    @RequestMapping("/add")
    public String add(AdDto adDto,ModelMap modelMap){
        if (adService.add(adDto)){
            modelMap.addAttribute(PageCodeEnum.KEY,PageCodeEnum.ADD_SUCCESS);
        }else {
            modelMap.addAttribute(PageCodeEnum.KEY,PageCodeEnum.ADD_FAIL);
        }
        return "/content/adAdd";
    }
}
